package com.piece.file.config;

import com.piece.core.framework.enums.StatusType;
import com.piece.core.framework.util.collection.CollectionUtil;
import com.piece.core.framework.util.file.FileUtil;
import com.piece.core.framework.util.object.BeanUtil;
import com.piece.file.model.TbFileClient;
import com.piece.file.service.TbFileClientService;
import com.piece.file.client.FileClientFactory;
import com.piece.file.client.FileClientFactoryImpl;
import com.piece.file.client.ftp.FtpFileClientConfig;
import com.piece.file.client.local.LocalFileClientConfig;
import com.piece.file.client.mongo.MongoFileClientConfig;
import com.piece.file.client.s3.S3FileClientConfig;
import com.piece.file.client.sftp.SftpFileClientConfig;
import com.piece.file.enums.FileStorageType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.List;

@Configuration
public class FileClientAutoConfig {

    private String clientType = "native";

    @Resource
    private TbFileClientService tbFileClientService;

    @Bean
    public FileClientFactory fileClientFactory() {
        FileClientFactory factory = new FileClientFactoryImpl(clientType);
        initFileClient(factory);
        return factory;
    }

    private void initFileClient(FileClientFactory fileClientFactory) {
        // 初始化本地文件客户端
        LocalFileClientConfig config = new LocalFileClientConfig();
        config.setBasePath(FileUtil.getStoragePath());
        fileClientFactory.init(FileStorageType.NATIVE.getName(), config);

        List<TbFileClient> clients = tbFileClientService.findAll();
        if (CollectionUtil.isNotEmpty(clients)) {
            FileStorageType storageType;
            for (TbFileClient client : clients) {
                if (StatusType.NORMAL.getCode().equals(client.getIsPrimary())) {
                    fileClientFactory.changeClientType(client.getClientType());
                }

                storageType = FileStorageType.getByStorage(client.getClientType());
                switch (storageType) {
                    case FTP:
                        FtpFileClientConfig config_ftp = new FtpFileClientConfig();
                        BeanUtil.copyProperties(client, config_ftp, true);
                        fileClientFactory.init(FileStorageType.FTP.getName(), config_ftp);
                        break;
                    case SFTP:
                        SftpFileClientConfig config_sftp = new SftpFileClientConfig();
                        BeanUtil.copyProperties(client, config_sftp, true);
                        fileClientFactory.init(FileStorageType.SFTP.getName(), config_sftp);
                        break;
                    case MONGO:
                        MongoFileClientConfig config_mongo = new MongoFileClientConfig();
                        BeanUtil.copyProperties(client, config_mongo, true);
                        fileClientFactory.init(FileStorageType.MONGO.getName(), config_mongo);
                        break;
                    case S3:
                        S3FileClientConfig config_s3 = new S3FileClientConfig();
                        BeanUtil.copyProperties(client, config_s3, true);
                        fileClientFactory.init(FileStorageType.S3.getName(), config_s3);
                        break;
                }
            }
        }
    }
}
